package cn.waka.sys.admin.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import cn.waka.sys.admin.object.Auth;
import cn.waka.sys.admin.object.Menu;
import cn.waka.sys.admin.object.Node;

@Mapper
public interface IMenuDao {

    List<Menu> findMenuTree(@Param("auths") String[] auths);

    @Select("select n.id,n.node,n.title from s_system_auth_node an left join s_system_node n on an.node_id=n.id where an.auth=#{authId}")
    List<Node> findByRoleId(int authId);

    @Select("SELECT m.id,m.title,m.url,m.icon,m.target,m.pid,m.sort,m.`status`,m.node as nodeId,n.id as `node.id`,n.title as `node.title` FROM s_system_menu m left join s_system_node n on m.node=n.id where m.id=#{id}")
    Menu findMenuById(int id);

    @Select("SELECT id,`status` FROM `s_system_role` WHERE id =#{id}")
    Auth findAuthById(int id);

    @Select("SELECT status FROM `s_system_role` WHERE id =#{id}")
    boolean findAuthStatus(String id);

    @Select("SELECT id,title,url,icon,target,pid,sort,`status`,node as nodeId FROM s_system_menu where status=1 ORDER BY pid,sort")
    List<Menu> selectedMenus();

    List<Menu> findMenus();

    @Insert("insert into s_system_menu(pid,node,title,url,icon)values(#{pid},#{nodeId},#{title},#{url},#{icon})")
    int insert(Menu menu);

    @Update("UPDATE s_system_menu SET pid=#{pid},node=#{nodeId},title=#{title},url=#{url},icon=#{icon} WHERE id=#{id}")
    int update(Menu menu);

    @Select("select count(1) from s_system_menu where pid=#{pid}")
    int findChildren(int pid);

    @Update("UPDATE s_system_menu SET `status`=#{status} WHERE id=#{id}")
    int updateStatus(@Param("status") int status, @Param("id") int id);

    @Update("UPDATE s_system_menu SET `sort`=#{sort} WHERE id=#{id}")
    int updateSort(@Param("sort") int sort, @Param("id") int id);

    @Delete("DELETE FROM s_system_menu WHERE id=#{id}")
    int del(int id);


}